package tree.interator;

import tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @ClassName layer
 * @Description TODO
 * @Author lenovo
 * @Date 2023-03-08 10:54
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class layerOrder {

    public List<List<Integer>> layerOrder(TreeNode node) {
        List<List<Integer>> res = new ArrayList<>();

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(node);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> tem = new ArrayList<>();
            while (size > 0) {
                TreeNode poll = queue.poll();
                tem.add(poll.val);
                if (poll.left != null) queue.add(poll.left);
                if (poll.right != null) queue.add(poll.right);
                size--;
            }
            res.add(tem);
        }
        return res;
    }


    public static void main(String[] args) {
        layerOrder layerOrder = new layerOrder();

        TreeNode treeNode = new TreeNode(2);

        TreeNode treeNode1 = new TreeNode(1);
        treeNode.left = treeNode1;

        TreeNode treeNode2 = new TreeNode(3);
        treeNode.right = treeNode2;


        List<List<Integer>> lists = layerOrder.layerOrder(treeNode);

        for (List<Integer> list : lists) {
            for (Integer integer : list) {
                System.out.println(integer);
            }
        }

    }
}